import 'package:flutter/material.dart';
import 'package:flutterlearn/animated_logo.dart';
import 'package:flutterlearn/log_widget.dart';
import 'package:flutterlearn/logo_app.dart';
import 'package:flutterlearn/photo_app_page.dart';
import 'package:flutterlearn/app_lifecycle.dart';
import 'package:flutterlearn/flutter_widget_lifecycle.dart';
import 'package:flutterlearn/gesture_page.dart';
import 'package:flutterlearn/flutter_layout_page.dart';
import 'package:flutterlearn/launch_page.dart';
import 'package:flutterlearn/photo_hero.dart';
import 'package:flutterlearn/radial_hero.dart';
import 'package:flutterlearn/resources_page.dart';
import 'package:flutterlearn/stateful_group_page.dart';
import 'package:flutterlearn/stateless_group_page.dart';
import 'package:flutterlearn/plugin_use.dart';
import 'package:transparent_image/transparent_image.dart';
import 'package:cached_network_image/cached_network_image.dart';

void main() => runApp(DynamicTheme());

class DynamicTheme extends StatefulWidget {
  @override
  _DynamicThemeState createState() => _DynamicThemeState();
}

class _DynamicThemeState extends State<DynamicTheme> {
  Brightness _brightness = Brightness.light;

  //字体下载地址：https://fonts.google.com/specimen/Rubik+Mono+One?selection.family=Rubik+Mono+One
  //更多参考：https://flutter.dev/docs/cookbook/design/fonts
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
//        fontFamily: 'RubikMonoOne', //将该字体应用到全局
        brightness: _brightness,
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
//        appBar: AppBar(
//          title: Text('使用Icons'),
//        ),
//        body: Center(
//          child: Icon(
//            Icons.android,
//            size: 100,
//          ),
//        ),
//        appBar: AppBar(
//          title: Text('如何配置图片缓存'),
//        ),
//        body: Center(
//          child: CachedNetworkImage(
//            placeholder: (context, url)=> CircularProgressIndicator(),
//            imageUrl: 'http://www.devio.org/img/avatar.png',
//          ),
//        ),
//        appBar: AppBar(
//          title: Text('从内存中加载placeholder'),
//        ),
//          body: Stack(
//            children: <Widget>[
//              Center(
//                child: CircularProgressIndicator(),
//              ),
//              Center(
//                child: FadeInImage.memoryNetwork(
//                  placeholder: kTransparentImage,
//                  image: 'http://www.devio.org/img/avatar.png',
//                ),
//              ),
//            ],
//          )
        appBar: AppBar(
          title: Text('如何创建和使用 Flutter 的路由与导航？'),
        ),
        body: Column(
          children: <Widget>[
            RaisedButton(
              onPressed: () {
                setState(() {
                  if (_brightness == Brightness.dark) {
                    _brightness = Brightness.light;
                  } else {
                    _brightness = Brightness.dark;
                  }
                });
              },
              child: Text(
                '切换主题 Hello',
                style: TextStyle(fontFamily: 'RubikMonoOne'), // 将该字体应用到局部
              ),
            ),
            RouteNavigator()
          ],
        ),
      ),
      routes: <String, WidgetBuilder>{
        'plugin': (BuildContext context) => PluginUse(),
        'less': (BuildContext context) => StatelessGroupPage(),
        'ful': (BuildContext context) => StatefulGroupPage(),
        'layout': (BuildContext context) => FlutterLayoutPage(),
        'gesture': (BuildContext context) => GesturePage(),
        'resources': (BuildContext context) => ResourcesPage(),
        'launch': (BuildContext context) => LaunchPage(),
        'widgetLifecycle': (BuildContext context) => FlutterWidgetLifecycle(),
        'appLifecycle': (BuildContext context) => AppLifecycle(),
        'photoApp': (BuildContext context) => PhotoAppPage(),
        'logoApp': (BuildContext context) => LogoApp(),
        'animatedLogo': (BuildContext context) => AnimatedLogo(),
        'logWidget': (BuildContext context) => LogWidget(),
        'photoHero': (BuildContext context) => PhotoHero(),
        'radialHero': (BuildContext context) => RadialHero(),
      },
    );
  }
}

class RouteNavigator extends StatefulWidget {
  @override
  _RouteNavigatorState createState() => _RouteNavigatorState();
}

class _RouteNavigatorState extends State<RouteNavigator> {
  bool byName = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        children: <Widget>[
          SwitchListTile(
              title: Text('${byName ? '' : '不'}通过路由跳转'),
              value: byName,
              onChanged: (value) {
                setState(() {
                  byName = value;
                });
              }),
          _item('如何使用Flutter包和插件？', PluginUse(), 'plugin'),
          _item('StatelessWidget与基础组件', StatelessGroupPage(), 'less'),
          _item('StatefulWidget与基组件', StatefulGroupPage(), 'ful'),
          _item('如何进行Flutter布局开发', FlutterLayoutPage(), 'layout'),
          _item('如何检测用户手势以及处理点击事件', GesturePage(), 'gesture'),
          _item('如何导入和使用Flutter的资源文件', ResourcesPage(), 'resources'),
          _item('如何打开第三方应用', LaunchPage(), 'launch'),
          _item('Flutter页面生命周期', FlutterWidgetLifecycle(), 'widgetLifecycle'),
          _item('Flutter应用生命周期', AppLifecycle(), 'appLifecycle'),
          _item('拍照APP开发', PhotoAppPage(), 'photoApp'),
          _item('Animated动画1', LogoApp(), 'logoApp'),
          _item('Animated动画2', AnimatedLogo(), 'animatedLogo'),
          _item('Animated动画3', LogWidget(), 'logWidget'),
          _item('基本Hero动画', MaterialApp(home: HeroAnimation(),), 'photoHero'),
          _item('径向Hero动画', MaterialApp(home: RadialExpansionDemo(),), 'radialHero'),
        ],
      ),
    );
  }

  _item(String title, page, String routeName) {
    return Container(
      child: RaisedButton(
        onPressed: () {
          if (byName) {
            Navigator.pushNamed(context, routeName);
          } else {
            Navigator.push(
                context, MaterialPageRoute(builder: (context) => page));
          }
        },
        child: Text(title),
      ),
    );
  }
}
